// 获取画布和上下文
const canvas = document.getElementById('game-canvas');
const ctx = canvas.getContext('2d');

// 画布尺寸
const CANVAS_WIDTH = 800;
const CANVAS_HEIGHT = 600;
canvas.width = CANVAS_WIDTH;
canvas.height = CANVAS_HEIGHT;

// 球员和羽毛球属性
const PLAYER_WIDTH = 10;
const PLAYER_HEIGHT = 100;
const SHUTTLECOCK_RADIUS = 10;

// 球员位置
let player1Y = (CANVAS_HEIGHT - PLAYER_HEIGHT) / 2;
let player2Y = (CANVAS_HEIGHT - PLAYER_HEIGHT) / 2;

// 羽毛球位置和速度
let shuttlecockX = CANVAS_WIDTH / 2;
let shuttlecockY = CANVAS_HEIGHT / 2;
let shuttlecockDx = 5;
let shuttlecockDy = 2;

// 得分
let player1Score = 0;
let player2Score = 0;
const player1ScoreElement = document.getElementById('player1-score');
const player2ScoreElement = document.getElementById('player2-score');

// 绘制球员
function drawPlayers() {
    ctx.fillStyle = 'blue';
    ctx.fillRect(0, player1Y, PLAYER_WIDTH, PLAYER_HEIGHT);
    ctx.fillRect(CANVAS_WIDTH - PLAYER_WIDTH, player2Y, PLAYER_WIDTH, PLAYER_HEIGHT);
}

// 绘制羽毛球
function drawShuttlecock() {
    ctx.beginPath();
    ctx.arc(shuttlecockX, shuttlecockY, SHUTTLECOCK_RADIUS, 0, Math.PI * 2);
    ctx.fillStyle = 'yellow';
    ctx.fill();
    ctx.closePath();
}

// 移动羽毛球
function moveShuttlecock() {
    shuttlecockX += shuttlecockDx;
    shuttlecockY += shuttlecockDy;

    // 边界检测（上下边界）
    if (shuttlecockY - SHUTTLECOCK_RADIUS < 0 || shuttlecockY + SHUTTLECOCK_RADIUS > CANVAS_HEIGHT) {
        shuttlecockDy = -shuttlecockDy;
    }

    // 碰撞检测（球员）
    if (
        (shuttlecockX - SHUTTLECOCK_RADIUS < PLAYER_WIDTH &&
            shuttlecockY > player1Y &&
            shuttlecockY < player1Y + PLAYER_HEIGHT) ||
        (shuttlecockX + SHUTTLECOCK_RADIUS > CANVAS_WIDTH - PLAYER_WIDTH &&
            shuttlecockY > player2Y &&
            shuttlecockY < player2Y + PLAYER_HEIGHT)
    ) {
        shuttlecockDx = -shuttlecockDx;
    }

    // 得分检测
    if (shuttlecockX - SHUTTLECOCK_RADIUS < 0) {
        player2Score++;
        player2ScoreElement.textContent = player2Score;
        resetShuttlecock();
    } else if (shuttlecockX + SHUTTLECOCK_RADIUS > CANVAS_WIDTH) {
        player1Score++;
        player1ScoreElement.textContent = player1Score;
        resetShuttlecock();
    }
}

// 重置羽毛球位置
function resetShuttlecock() {
    shuttlecockX = CANVAS_WIDTH / 2;
    shuttlecockY = CANVAS_HEIGHT / 2;
    shuttlecockDx = -shuttlecockDx;
    shuttlecockDy = 2;
}

// 处理键盘事件控制球员移动
document.addEventListener('keydown', function (event) {
    if (event.key === 'w' && player1Y > 0) {
        player1Y -= 10;
    } else if (event.key === 's' && player1Y < CANVAS_HEIGHT - PLAYER_HEIGHT) {
        player1Y += 10;
    } else if (event.key === 'ArrowUp' && player2Y > 0) {
        player2Y -= 10;
    } else if (event.key === 'ArrowDown' && player2Y < CANVAS_HEIGHT - PLAYER_HEIGHT) {
        player2Y += 10;
    }
});

// 主游戏循环
function gameLoop() {
    ctx.clearRect(0, 0, CANVAS_WIDTH, CANVAS_HEIGHT);

    drawPlayers();
    drawShuttlecock();
    moveShuttlecock();

    requestAnimationFrame(gameLoop);
}

// 初始化游戏
gameLoop();